﻿namespace YDT_ProductService.Domains.Entities
{
    /// <summary>
    /// 商品图片实体 - 属于商品聚合的一部分
    /// </summary>
    public class ProductImage : Entity
    {
        /// <summary>商品ID - 外键</summary>
        public Guid ProductId { get; private set; }
        /// <summary>图片排序序号</summary>
        public int ImageSort { get; private set; }
        /// <summary>图片状态（1：启用，2：禁用）</summary>
        public string ImageStatus { get; private set; }
        /// <summary>图片URL</summary>
        public string ImageUrl { get; private set; }
        /// <summary>创建时间</summary>
        public DateTime CreateTime { get; private set; }

        /// <summary>
        /// 受保护的构造函数 - 用于EF Core
        /// </summary>
        protected ProductImage() { }

        /// <summary>
        /// 创建商品图片构造函数
        /// </summary>
        public ProductImage(Guid productId, string imageUrl, int imageSort = 0, string imageStatus = "1")
        {
            Id = Guid.NewGuid();
            ProductId = productId;
            ImageUrl = imageUrl;
            ImageSort = imageSort;
            ImageStatus = imageStatus;
            CreateTime = DateTime.UtcNow;

            ValidateCreation();
        }

        /// <summary>
        /// 更新图片排序
        /// </summary>
        public void UpdateSort(int newSort)
        {
            ImageSort = newSort;
        }

        /// <summary>
        /// 启用图片
        /// </summary>
        public void Enable()
        {
            ImageStatus = "1";
        }

        /// <summary>
        /// 禁用图片
        /// </summary>
        public void Disable()
        {
            ImageStatus = "2";
        }

        /// <summary>
        /// 验证图片创建时的业务规则
        /// </summary>
        private void ValidateCreation()
        {
            if (string.IsNullOrWhiteSpace(ImageUrl))
                throw new ArgumentException("图片URL不能为空");

            if (ImageSort < 0)
                throw new ArgumentException("图片排序不能为负数");
        }
    }
}
